import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * 主的UI 界面
 * @Author: 贺坤
 * @Date: 2021/5/20 8:55
 */
public class Ui extends JFrame {
    // 设置界面大小
    private static final Dimension SIZE = new Dimension(600, 400);
    private JTextArea statusBar;
    private JScrollPane jScrollPane;
    // 定义3个按钮
    private JButton srcBtn, targetBtn, performBtn;
    private File srcDir;
    private File targetDir;
    private CopyWorker copyWorker;

    @Override
    public Dimension getMinimumSize() {
        return SIZE;
    }

    @Override
    public Dimension getMaximumSize() {
        return SIZE;
    }

    @Override
    public Dimension getPreferredSize() {
        return SIZE;
    }

    @Override
    public String getTitle() {
        return "文件复制器";
    }


    public Ui() throws HeadlessException {
        super();
        init();
        addListeners();
        doLay();
    }

    private void init() {
        // 设置关闭机制
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 初始化一个文字提示器
        statusBar = new JTextArea();
        // 将文本域放在滚动窗口里
        jScrollPane =new JScrollPane(statusBar);
        // 初始化3个按钮
        srcBtn = new JButton("源路径...");
        targetBtn = new JButton("目标路径...");
        performBtn = new JButton("复制") {
            @Override
            public Color getForeground() {
                return Color.BLUE;
            }
        };
        // 初始化复制工作者
        copyWorker = new CopyWorker(statusBar);
    }

    private void doLay() {
        Container container = getContentPane();
        container.setLayout(new GridLayout(0, 1));
        container.add(srcBtn);
        container.add(targetBtn);
        container.add(performBtn);
        container.add(jScrollPane);

        pack();
        setVisible(true);
    }

    private void addListeners() {
        // 目标按钮添加 操作监听器 一个行为
        targetBtn.addActionListener(new AbstractAction() {
            //文件选择器
            private JFileChooser fileChooser;
            // 执行的操作
            @Override
            public void actionPerformed(ActionEvent e) {
                if (fileChooser == null) {
                    fileChooser = new JFileChooser();
                    //设置可以选择目录
                    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    // 设置打开对话框
                    fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
                    // 设置文件筛选器
                    fileChooser.setFileFilter(new FileFilter() {

                        @Override
                        public boolean accept(File f) {
                            return f.isDirectory();
                        }

                        @Override
                        public String getDescription() {
                            return "目标路径";
                        }
                    });
                }

                if (fileChooser.showOpenDialog(Ui.this) == JFileChooser.APPROVE_OPTION) {
                    targetDir = fileChooser.getSelectedFile();
                }

            }
        });


        srcBtn.addActionListener(new AbstractAction() {
            private JFileChooser fileChooser;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (fileChooser == null) {
                    fileChooser = new JFileChooser();
                    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
                    fileChooser.setFileFilter(new FileFilter() {

                        @Override
                        public boolean accept(File f) {
                            return true;
                        }

                        @Override
                        public String getDescription() {
                            return "源路径";
                        }
                    });
                }

                if (fileChooser.showOpenDialog(Ui.this) == JFileChooser.APPROVE_OPTION) {
                    srcDir = fileChooser.getSelectedFile();
                }

            }
        });
        // 设置复制按钮 事件
        performBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                copyWorker.copyInit(srcDir,targetDir);
                copyWorker.execute();
            }
        });
    }
}
